<?php

	include_once("MIME/Type.php");

	function iconForMimeType($type, $iconsize = "small") {
		switch ($iconsize) {
			case "large":
				$imagebeg = "iconlarge";
			break;
			default:
				$imagebeg = "icon";
			break;
		}
		if ($iconsize == "small") {
			$folder = "icon";
		}
		else {
			$folder = "iconlarge";
		}

		switch($type){
			case 'image/jpeg':
			case 'image/pjpeg':
			case 'image/gif':
			case 'image/png':
			case 'image/tiff':
				$iconUrl = '/cms/html/files/images/'.$imagebeg.'_imglist.gif';				
			break;
						
			case 'application/pdf':
				$iconUrl = '/cms/html/files/images/'.$imagebeg.'_view_pdf.gif';
			break;
			
			case 'application/msword':
				$iconUrl = '/cms/html/files/images/'.$imagebeg.'_view_word.gif';
			break;
			
			case 'application/vnd.ms-excel':
				$iconUrl = '/cms/html/files/images/'.$imagebeg.'_view_excel.gif';
			break;
			
			default:
				$iconUrl = '/cms/html/files/images/'.$imagebeg.'_filelist.gif';	
		}
		return $iconUrl;
	}
		
	// this is a simple helper to quickly lookup extensions for mime types.
	// for known image types .
	function imageFileExtensionForMimeType($imageMimeType) {
		switch ($imageMimeType){
			case 'image/gif':
				return "gif";
			break;
			case 'image/jpeg':
			case 'image/pjpeg':
				return "jpg";
			break;
			case 'image/tiff':
				return "tiff";
			break;
			case 'image/x-png':
				return "png";
			break;
			case 'image/x-xbitmap':
				return "xbm";
			break;
			case 'image/bmp':
				return "bmp";
			break;
			default:
				return "-";
			break;
		}
	}
	
	
	function getFileDetails(&$document, &$db){
		$attachmentDetails=null;
		$db->executeQuery("SELECT id, filename, filesize, mimetype FROM binarydata WHERE id=" . $document->content ." AND filetypecode=1 AND documentcode=". $document->documentcode . "");//there should be an element type here, but it is not majorly important as there is only one blob per record
		if (!($attachmentDetails = $db->nextObject())) {
			return null;
		}
		return $attachmentDetails;		
	}
	
	function isMimeTypeAllowed ($types, $typeToMatch) {
		$supported =false;
		foreach($types as $pattern) {
			$supported = MIME_Type::wildcardMatch($pattern, $typeToMatch);		
			if ($supported) break;
		}
		return $supported;
	}
?>